package cn.mrcode.fd.blog.controller;

import cn.mrcode.fd.blog.buutil.FilePathUtil;
import cn.mrcode.fd.blog.buutil.ImgPathInfo;
import cn.mrcode.fd.blog.common.BaseController;
import cn.mrcode.fd.blog.entityCu.UserCu;
import cn.mrcode.fd.utils.UUID;
import cn.mrcode.fd.utils.io.FileUploadUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;

/**
 * 公用服务
 *
 * @author zhuqiang
 * @version V1.0
 * @date 2015/12/6 15:13
 */
@RequestMapping("/csv")
@Controller
public class CommonSVController extends BaseController{
    @RequestMapping(value = "/image_upload", produces = "application/json;charset=utf-8")
    @ResponseBody
    public String imageUpload(HttpServletRequest request) {
        UserCu user = getUser();
        JSONObject result = new JSONObject();
        ImgPathInfo imgPathInfo = FilePathUtil.generImgPathArticleInfo(UUID.uuid() + ".jpg", user.getAccount());
        boolean flag = FileUploadUtil.springUpImg(request, imgPathInfo.getAbsFolderPath(), imgPathInfo.getImgName(),true);
        if (!flag) {
            result.put("success", 0);
            result.put("message", "上传失败");
            return result.toJSONString();
        }
        result.put("success",1);
        result.put("message","上传成功");
        result.put("url",imgPathInfo.getRelDbImgPath());
        return result.toString();
    }
}
